package string;

/**
 * @Author: xpengfei
 * @Date: 2019/9/26 14:46
 * @Description：评测10ms，超越35%；空间复杂度超越75%；待优化
 */
public class Solution38 {
    public String countAndSay(int n) {
        if(n == 1){
            return "1";
        }else{
            String temp = countAndSay(n-1);
            String result = "";
            char ch = temp.charAt(0);
            int num = 0;
            for (int i = 0; i < temp.length(); i++) {
                if (ch == temp.charAt(i)){
                    num++;
                    continue;
                }
                result +=num+""+ch;
                num = 1;
                ch = temp.charAt(i);
            }
            result += String.valueOf(num)+String.valueOf(ch);
            return result;
        }
    }


    public static void main(String[] args) {
        Solution38 solution38 = new Solution38();
        System.out.println("result:"+solution38.countAndSay(
                7));
    }
}
